package com.wxxymaker.model.dao;

import com.wxxymaker.model.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface UserDao extends JpaRepository<User, Integer>, JpaSpecificationExecutor<User> {

    /**
     * 通过账号和密码查询用户，用于用户的登录
     */
    @Query(value = "select * from t_user where binary username=?1 and password=?2", nativeQuery = true)
    User findByUsernameAndPassword(String username, String password);

    /**
     * 通过账号查询用户，用于判断账号是否被占用
     */
    @Query(value = "select * from t_user where binary username=?1", nativeQuery = true)
    User findByUsername(String username);

    /**
     * 通过用户名进行模糊查询，用于用户搜索
     */
    List<User> findByRealNameLike(String name);

    /**
     * 增加指定用户的参评次数
     * @param id 用户id
     */
    @Modifying
    @Query("update User set num = num + 1 where id = ?1")
    int addNum(Integer id);

    int deleteAllByUsername(String username);
}
